package com.android.gtravelling;

import java.util.ArrayList;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.util.Log;

import com.android.dto.HotelsDTO;
import com.android.dto.ImagesDTO;
import com.android.dto.PlacesDTO;
import com.android.dto.ProvincesDTO;

public class WebserviceCaller {
	private final String NAMESPACE = "http://internship.org/";
	// private final String URL = "http://10.0.2.2:50761/Service1.asmx?WSDL";
	private final String URL = "http://10.0.2.2:50383/GTravellingService.asmx?WSDL";

	public boolean connectSQL() {
		boolean res = false;

		final String SOAP_ACTION = "http://internship.org/ConnectionSQL";
		final String METHOD_NAME = "ConnectionSQL";
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true; // put this only if the web service is .NET one
		envelope.setOutputSoapObject(request);
		HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
		try {
			androidHttpTransport.call(SOAP_ACTION, envelope);
			SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
			Log.i("myApp", response.toString());
			if (response.toString().equalsIgnoreCase("true")) {
				res = true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return res;
	}

	public String SayHello() {
		String str = "";
		final String SOAP_ACTION = "http://internship.org/HelloWorld";
		final String METHOD_NAME = "HelloWorld";
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true; // put this only if the web service is .NET one
		envelope.setOutputSoapObject(request);
		HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
		try {
			androidHttpTransport.call(SOAP_ACTION, envelope);
			SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
			str = response.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return str;
	}

	/**
	 * @return
	 */
	public ArrayList<ProvincesDTO> selectAllProvinces() {
		ArrayList<ProvincesDTO> lst = new ArrayList<ProvincesDTO>();
		final String SOAP_ACTION = "http://internship.org/GetAllProvince";
		final String METHOD_NAME = "GetAllProvince";
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

		SoapObject table = null;
		SoapObject responseBody = null;

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

		try {

			androidHttpTransport.call(SOAP_ACTION, envelope);
			responseBody = (SoapObject) envelope.getResponse();

			responseBody = (SoapObject) responseBody.getProperty(1);
			table = (SoapObject) responseBody.getProperty(0);

			for (int i = 0; i < table.getPropertyCount(); i++) {
				ProvincesDTO dto = new ProvincesDTO();
				SoapObject row = (SoapObject) table.getProperty(i);
				dto.setId(Integer.valueOf(row.getProperty("Id").toString()));
				dto.setName(row.getProperty("Name").toString());
				dto.setDescription(row.getProperty("Description").toString());
				dto.setOrder(Integer.valueOf(row.getProperty("Order")
						.toString()));
				if(row.hasProperty("URL")==true)
				dto.setImgURL(row.getProperty("URL").toString());
				
				lst.add(dto);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return lst;
	}

	public ArrayList<PlacesDTO> selectAllPlaces() {
		ArrayList<PlacesDTO> lst = new ArrayList<PlacesDTO>();
		final String SOAP_ACTION = "http://internship.org/GetAllPlaces";
		final String METHOD_NAME = "GetAllPlaces";
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

		SoapObject table = null;
		SoapObject responseBody = null;

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

		try {

			androidHttpTransport.call(SOAP_ACTION, envelope);
			responseBody = (SoapObject) envelope.getResponse();

			responseBody = (SoapObject) responseBody.getProperty(1);
			table = (SoapObject) responseBody.getProperty(0);

			for (int i = 0; i < table.getPropertyCount(); i++) {
				PlacesDTO dto = new PlacesDTO();
				SoapObject row = (SoapObject) table.getProperty(i);
				dto.setId(Integer.valueOf(row.getProperty("Id").toString()));
				dto.setName(row.getProperty("Name").toString());
				dto.setDescription(row.getProperty("Description").toString());
				dto.setAddress(row.getProperty("Address").toString());

				lst.add(dto);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return lst;
	}

	public PlacesDTO selectOnePlace(int pID) {
		PlacesDTO place = new PlacesDTO();
		final String SOAP_ACTION = "http://internship.org/GetOnePlace";
		final String METHOD_NAME = "GetOnePlace";
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

		SoapObject table = null;
		SoapObject client = null;
		SoapObject tableRow = null;
		SoapObject responseBody = null;

		PropertyInfo propInfo1 = new PropertyInfo();
		propInfo1.setName("pID");
		propInfo1.setValue(pID);
		propInfo1.setType(Integer.class);
		request.addProperty(propInfo1);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

		try {

			androidHttpTransport.call(SOAP_ACTION, envelope);
			responseBody = (SoapObject) envelope.getResponse();

			responseBody = (SoapObject) responseBody.getProperty(1);
			table = (SoapObject) responseBody.getProperty(0);

			tableRow = (SoapObject) table.getProperty(0);
			place.setId(Integer.valueOf(tableRow.getProperty("Id").toString()));
			place.setName(tableRow.getProperty("Name").toString());
			place.setDescription(tableRow.getProperty("Description").toString());

		} catch (Exception e) {
			e.printStackTrace();
		}
		return place;
	}

	public ArrayList<Employee> selectAllUsers() {
		String count = "";
		ArrayList<Employee> lst = new ArrayList<Employee>();
		final String SOAP_ACTION = "http://internship.org/GetAllUser";
		final String METHOD_NAME = "GetAllUser";
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

		SoapObject table = null;
		SoapObject client = null;
		SoapObject tableRow = null;
		SoapObject responseBody = null;

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

		try {

			androidHttpTransport.call(SOAP_ACTION, envelope);
			responseBody = (SoapObject) envelope.getResponse();

			responseBody = (SoapObject) responseBody.getProperty(1);
			table = (SoapObject) responseBody.getProperty(0);

			for (int i = 0; i < table.getPropertyCount(); i++) {
				Employee dto = new Employee();
				String x = "";
				SoapObject row = (SoapObject) table.getProperty(i);
				dto.setName(row.getProperty("name").toString());
				dto.setPhone(row.getProperty("phone").toString());
				dto.setEmail(row.getProperty("email").toString());
				x.trim();
				lst.add(dto);
			}
		} catch (Exception e) {
			e.printStackTrace();
			count = e.toString();
		}
		return lst;
	}

	public ProvincesDTO selectOneProvince(int pID) {
		ProvincesDTO prov = new ProvincesDTO();
		final String SOAP_ACTION = "http://internship.org/GetOneProvince";
		final String METHOD_NAME = "GetOneProvince";
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

		SoapObject table = null;
		SoapObject client = null;
		SoapObject tableRow = null;
		SoapObject responseBody = null;

		PropertyInfo propInfo1 = new PropertyInfo();
		propInfo1.setName("pID");
		propInfo1.setValue(pID);
		propInfo1.setType(Integer.class);
		request.addProperty(propInfo1);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

		try {

			androidHttpTransport.call(SOAP_ACTION, envelope);
			responseBody = (SoapObject) envelope.getResponse();

			responseBody = (SoapObject) responseBody.getProperty(1);
			table = (SoapObject) responseBody.getProperty(0);

			tableRow = (SoapObject) table.getProperty(0);
			prov.setId(Integer.valueOf(tableRow.getProperty("Id").toString()));
			prov.setName(tableRow.getProperty("Name").toString());
			prov.setDescription(tableRow.getProperty("Description").toString());
			prov.setOrder(Integer.valueOf(tableRow.getProperty("Order")
					.toString()));
			if(tableRow.hasProperty("URL"))
			prov.setImgURL(tableRow.getProperty("URL").toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return prov;
	}

	public Employee selectOneuser(String paccount) {
		Employee emp = new Employee();
		final String SOAP_ACTION = "http://internship.org/selectOneuser";
		final String METHOD_NAME = "selectOneuser";
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

		SoapObject table = null;
		SoapObject client = null;
		SoapObject tableRow = null;
		SoapObject responseBody = null;

		PropertyInfo propInfo1 = new PropertyInfo();
		propInfo1.setName("paccount");
		propInfo1.setValue(paccount);
		propInfo1.setType(String.class);
		request.addProperty(propInfo1);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

		try {

			androidHttpTransport.call(SOAP_ACTION, envelope);
			responseBody = (SoapObject) envelope.getResponse();

			responseBody = (SoapObject) responseBody.getProperty(1);
			table = (SoapObject) responseBody.getProperty(0);

			tableRow = (SoapObject) table.getProperty(0);
			emp.setName(tableRow.getProperty("account").toString());
			emp.setPhone(tableRow.getProperty("phone").toString());
			emp.setEmail(tableRow.getProperty("email").toString());

		} catch (Exception e) {
			e.printStackTrace();
		}
		return emp;
	}

	// take a database from webserver to Arraylist
	public ArrayList<HotelsDTO> selectAllHotels() {
		ArrayList<HotelsDTO> lst = new ArrayList<HotelsDTO>();
		final String SOAP_ACTION = "http://internship.org/GetAllHotel";
		final String METHOD_NAME = "GetAllHotel";
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

		SoapObject table = null;
		SoapObject responseBody = null;

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

		try {

			androidHttpTransport.call(SOAP_ACTION, envelope);
			responseBody = (SoapObject) envelope.getResponse();

			responseBody = (SoapObject) responseBody.getProperty(1);
			table = (SoapObject) responseBody.getProperty(0);

			for (int i = 0; i < table.getPropertyCount(); i++) {
				HotelsDTO dto = new HotelsDTO();
				SoapObject row = (SoapObject) table.getProperty(i);
				// Integer.valueOf :convert String into interger
				dto.setId(Integer.valueOf(row.getProperty("Id").toString()));
				dto.setName(row.getProperty("Name").toString());
				dto.setAddress(row.getProperty("Address").toString());
				dto.setPhones(row.getProperty("Phones").toString());
				dto.setDescription(row.getProperty("Description").toString());

				lst.add(dto);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return lst;
	}

	// to display detail of hotel which was chosen in list
	public HotelsDTO selectOneHotels(int pID) {
		HotelsDTO dto = new HotelsDTO();
		final String SOAP_ACTION = "http://internship.org/GetOneHotel";
		final String METHOD_NAME = "GetOneHotel";
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

		SoapObject table = null;
		SoapObject responseBody = null;

		PropertyInfo propInfo1 = new PropertyInfo();
		propInfo1.setName("pID");
		propInfo1.setValue(pID);
		propInfo1.setType(Integer.class);
		request.addProperty(propInfo1);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

		try {

			androidHttpTransport.call(SOAP_ACTION, envelope);
			responseBody = (SoapObject) envelope.getResponse();

			responseBody = (SoapObject) responseBody.getProperty(1);
			table = (SoapObject) responseBody.getProperty(0);
			SoapObject row = (SoapObject) table.getProperty(0);

			dto.setId(Integer.valueOf(row.getProperty("Id").toString()));
			dto.setName(row.getProperty("Name").toString());
			dto.setAddress(row.getProperty("Address").toString());
			dto.setPhones(row.getProperty("Phones").toString());
			dto.setDescription(row.getProperty("Description").toString());

		} catch (Exception e) {
			e.printStackTrace();
		}
		return dto;
	}

	
	/**
	 * Get all the image's urls for any place indicated by pID
	 * @param pID
	 * @return
	 */
	public ArrayList<ImagesDTO> selectAllImageOfPlace(int pID) {
		ArrayList<ImagesDTO> lst = new ArrayList<ImagesDTO>();
		final String SOAP_ACTION = "http://internship.org/GetAllImageOfPlace";
		final String METHOD_NAME = "GetAllImageOfPlace";
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

		SoapObject table = null;
		SoapObject responseBody = null;

		PropertyInfo propInfo1 = new PropertyInfo();
		propInfo1.setName("pID");
		propInfo1.setValue(pID);
		propInfo1.setType(Integer.class);
		request.addProperty(propInfo1);
		
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

		try {

			androidHttpTransport.call(SOAP_ACTION, envelope);
			responseBody = (SoapObject) envelope.getResponse();

			responseBody = (SoapObject) responseBody.getProperty(1);
			table = (SoapObject) responseBody.getProperty(0);

			for (int i = 0; i < table.getPropertyCount(); i++) {
				ImagesDTO dto = new ImagesDTO();
				SoapObject row = (SoapObject) table.getProperty(i);
				// Integer.valueOf :convert String into interger
				dto.setId(Integer.valueOf(row.getProperty("Id").toString()));
				dto.setName(row.getProperty("Name").toString());
				dto.setUrl(row.getProperty("URL").toString());
				dto.setOrder(Integer.valueOf(row.getProperty("Order").toString()));
				dto.setDescription(row.getProperty("Description").toString());
				dto.setDescription(row.getProperty("Description").toString());
				lst.add(dto);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return lst;
	}
}